﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Renren.Api;
using System.Collections.Generic;
using Renren.Schema;
using System.Text;

public partial class ajax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/html";
        StringBuilder sb = new StringBuilder();
        //Client client = Session["xnClient"] as Client;

            ApiSettings setting = new ApiSettings("0e91c1474004445a9f4a633706f19bf1", "e30ecf448e21410e85d9501bdeddf0da");
                    UserSession session = new UserSession();
            IParser parser = new XmlParser();
            Client client = new Client(setting, session, parser);

  











        if (client != null)
        {
            List<user> lu = getUserList(client);

            sb.Append("({\"res\":[");
            int count = lu.Count;
            int cur = 0;
            foreach (user var in lu)
            {
                sb.Append("{");
                sb.AppendFormat("\"birthday\":\"{0}\",", var.birthday);
                sb.AppendFormat("\"uid\":\"{0}\",", var.uid);
                sb.AppendFormat("\"name\":\"{0}\",", var.name);
                sb.AppendFormat("\"headurl\":\"{0}\"", var.headurl);

                if (cur++ < count - 1)
                {

                    sb.Append("},");
                }
                else
                {

                    sb.Append("}");
                }
            }
            sb.Append("]})");

            Response.Write(sb.ToString());
        }
        else
        {
            Response.Write("会话中断，原因：ajax.aspx请求session['xnClient']为空");
        }
       
    }

    public List<user> getUserList(Client client)
    {        
        if (Session["UserList"] == null)
        {

            List<int> friendList = client.Friends_Get();

            int[] friends = new int[friendList.Count];
            friendList.CopyTo(friends);

            List<user> temp = client.Users_GetInfo("uid,name,birthday,headurl", friends);
            Session["UserList"] = temp;
            return temp;
        }
        else
        {
            return (List<user>)Session["UserList"];
        }


    }
}
